<template>
    <div>
        <h2>管理员----修改投资</h2>
        <table class="table table-bordered">
            <tr>
                <td>投资名称：</td>
                <td><input type="text"  v-model="state.I_Name" /></td>
            </tr>
            <tr>
                <td>投资总金额：</td>
                <td><input type="text"  v-model="state.I_Budget" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" @click="AddInvest" value="修改" class="btn btn-primary" />
                <input type="hidden" v-model="state.I_Id"  /></td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import{reactive,ref,toRefs,onMounted} from 'vue'
import { useRouter,useRoute } from 'vue-router'
import axios from 'axios'

let router=useRouter()
let route=useRoute()

let data=reactive({
    state:{
        I_Name:'',
        I_Budget:'',
        I_Id:route.query.id
    }
})
let {state}=toRefs(data)
onMounted(()=>{
    fan();
})

const fan=()=>{
    axios({
        url:"/api/Invest/GetInvestitem",
        method:'get',
        params:{id:route.query.id}
    })
    .then(res=>{
        console.log(res.data.data);
        state.value.I_Name=res.data.data.I_Name;
        state.value.I_Budget=res.data.data.I_Budget;
    })
    .catch(err=>{
        console.log(err);
    })
}
const AddInvest=()=>{
    //验证
    if(state.value.I_Name==""||state.value.I_Budget=="")
        {
            alert("投资名称或投资总金额不能为空");
            return;
        }
        if(isNaN(Number(state.value.I_Budget))==true)
        {
            alert("投资总金额必须为数字");
            return;
        }
        if(Number(state.value.I_Budget)<100000||Number(state.value.I_Budget)>1000000)
        {
            alert("投资总金额必须大于10万小于100万");
            return;
        }
    axios({
        url:"/api/Invest/PostUpdInvest",
        method:'post',
        data:state.value
    })
    .then(res=>{
        console.log(res.data.data);
         if(res.data.data>0)
        {
            alert("修改投资成功");
            router.push('/main/adminInvestList')
        }
         else  if(res.data.data==-1)
        {
            alert("有人投资不能修改");
           return;
        }
        else
        {
            alert("修改失败");
            return;
        }
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style>

</style>